package com.tgpms.web.earlierStage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.dao.DocSortTemplateMapper;
import com.tgpms.web.earlierStage.dao.TaskDefinitionTemplateMapper;
import com.tgpms.web.earlierStage.entity.TaskDefinitionTemplate;
import com.tgpms.web.earlierStage.service.EarlierTaskDefinTempService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class EarlierTaskDefinTempServiceImpl implements EarlierTaskDefinTempService {

	@Autowired
	private TaskDefinitionTemplateMapper taskDefinitionTemplateMapper;
	@Autowired
	private DocSortTemplateMapper docSortTemplateMapper;

	@Override
	public synchronized Result addTaskDefinTemp(TaskDefinitionTemplate taskDefinTemp, Result result) {
		//主键形式 yyyyMMddHHmmss+UUID 
		taskDefinTemp.setTaskId(Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
		taskDefinTemp.setRowstamp(taskDefinTemp.getTaskId());
		taskDefinTemp.setInsertDate(new Date());
		if(Common.isEmpty(taskDefinTemp.getBeforeTask())) {
			taskDefinTemp.setBeforeTask("0");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		//判断增加的是父级任务还是子任务
		if(Common.isEmpty(taskDefinTemp.getParentId())) {
			taskDefinTemp.setParentId("0");
			//父任务
			map.put("parentId", "0");
			map.put("orderby", " order by TASK_CODE desc");
			List<TaskDefinitionTemplate> list = taskDefinitionTemplateMapper.selTaskDefinitionTemplate(map);
			String code = "0";
			if(list==null || list.size()==0) {
				code = "01";
			}else {
				int i = Integer.parseInt(list.get(0).getTaskCode())+1;
				if(i<10) {
					code = "0"+i;
				}else {
					code = i+"";
				}
			}
			taskDefinTemp.setTaskCode(code);
		}
		else {
			//子任务
			TaskDefinitionTemplate temp = taskDefinitionTemplateMapper.selectByPrimaryKey(taskDefinTemp.getParentId());
			if(temp==null) {
				result.setSuccess(false);
				result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain()+"|父任务为空");
				return result;
			}
			else {
				/**
				 * 自动编码
				 */
				String parentCode = temp.getTaskCode();
				map.clear();
				map.put("taskCodelike", parentCode);
				map.put("taskCodeno", parentCode);
				map.put("orderby", " order by TASK_CODE desc");
				List<TaskDefinitionTemplate> list2 = taskDefinitionTemplateMapper.selTaskDefinitionTemplate(map);
				String code = "0";
				if(list2==null || list2.size()==0) {
					code=parentCode+"01";
				}else {
					int i = parentCode.length();
					StringBuilder taskcode = new StringBuilder(list2.get(0).getTaskCode());
					int k = Integer.parseInt(taskcode.replace(0, i,"").toString())+1;
					String c = "";
					if(k<10) {
						c = "0"+k;
					}else {
						c = ""+k;
					}
					code = parentCode+c;
				}
				taskDefinTemp.setTaskCode(code);
			}
		}

		int i = taskDefinitionTemplateMapper.insertSelective(taskDefinTemp);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			result.setData(taskDefinTemp.getTaskId());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}

	@Override
	public Result updateTaskDefinTemp(TaskDefinitionTemplate taskDefinTemp, Result result) {
		taskDefinTemp.setModifyDate(new Date());
		int i = taskDefinitionTemplateMapper.updateByPrimaryKeySelective(taskDefinTemp);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}

	@Override
	public Result deleteTaskDefinTemp(String taskId, Result result) {
		//主键查询
		TaskDefinitionTemplate definitionTemp = taskDefinitionTemplateMapper.selectByPrimaryKey(taskId);
		int i = taskDefinitionTemplateMapper.deleteByPrimaryKey(taskId);
		if(i==1) {
			//删除下级模板
			taskDefinitionTemplateMapper.deleteTaskDefinitionTemplateByCode(definitionTemp.getTaskCode());
			//删除对应文档模板
			docSortTemplateMapper.deleteDocSortTempByTaskId(taskId);
			result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}
	

	@Override
	public List<Object> selTaskDefinTemp(String taskId) {
		//查询第一层
		List<Object> list = new ArrayList<Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if(!Common.isEmpty(taskId)) {
			map.put("parentId", taskId);
		}else {
			map.put("parentId", "0");
		}
		List<TaskDefinitionTemplate> onelist = taskDefinitionTemplateMapper.selTaskDefinitionTemplate(map); 
		if(onelist!=null && onelist.size()>0) {
			for(TaskDefinitionTemplate temp : onelist) {
				JSONObject tempJson = (JSONObject) JSONObject.toJSON(temp);
				tempJson.remove("insertUser");
				tempJson.remove("insertDate");
				tempJson.remove("modifyUser");
				tempJson.remove("modifyDate");
				tempJson.remove("rowstamp");
				Map<String, Object> map1 = new HashMap<String, Object>();
				map1.put("parentId", temp.getTaskId());
				List<Object> children = getChilren(map1);
				tempJson.put("children", children);

				list.add(tempJson);
			}
		}
		return list;
	}

	private List<Object> getChilren(Map<String, Object> map){
		List<Object> list = new ArrayList<Object>();
		List<TaskDefinitionTemplate> onelist = taskDefinitionTemplateMapper.selTaskDefinitionTemplate(map); 
		if(onelist!=null && onelist.size()>0) {
			for(TaskDefinitionTemplate temp : onelist) {
				JSONObject tempJson = (JSONObject) JSONObject.toJSON(temp);
				tempJson.remove("insertUser");
				tempJson.remove("insertDate");
				tempJson.remove("modifyUser");
				tempJson.remove("modifyDate");
				tempJson.remove("rowstamp");
				Map<String, Object> map1 = new HashMap<String, Object>();
				map1.put("parentId", temp.getTaskId());
				List<Object> children = getChilren(map1);
				tempJson.put("children", children);

				list.add(tempJson);
			}
		}
		return list;

	}

}
